package cn.kgc.ssm.web.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.ssm.web.entity.Slide;
import cn.kgc.ssm.web.response.ResponseResult;
import cn.kgc.ssm.web.service.SlideService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;

/**
 * @author 课工场
 * @date 2024/8/1
 * @description 轮播模块
 */
@RestController
@RequestMapping("slide")
public class SlideController {

    @Autowired
    private SlideService slideService;

    // 分页
    @GetMapping("page")
    public ResponseResult slidePage(Integer page, Integer limit) {
        return slideService.slidePage(page,limit);
    }

    // 图片上传      上传文件时  请求方式需要设置为post请求
    @PostMapping("upload")
    public ResponseResult uploadImage(MultipartFile file) throws Exception {

        if (file.isEmpty()){
            return ResponseResult.fail().message("请选择需要上传的文件");
        }
        //  1.处理文件的名字
        String originalFilename = file.getOriginalFilename();  //  文件原名   su7.jpg
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);  //  jpg
        String uuid = IdUtil.simpleUUID();
        String uuidName= uuid+"."+filenameExtension;

        // 2.上传文件
        File f = new File("D:\\upload\\slides\\" + uuidName);
        file.transferTo(f);

        return ResponseResult.success().data(uuidName);
    }

    // 保存轮播图基本信息
    @PostMapping("uploadInfo")
    public ResponseResult uploadInfo(Slide slide){

        return slideService.addUploadInfo(slide);
    }


    //  根据图片的名字 返回图片      4b38bc48ded74731955902ff5592bd82.jpg
    @GetMapping("getSlidePicture")
    public void getSlidePicture(String uuid, HttpServletResponse response) throws Exception{

        // 1. 将图片从磁盘读取为一个文件流
        FileInputStream fileInputStream = new FileInputStream("D:\\upload\\slides\\" + uuid);
        ServletOutputStream outputStream = response.getOutputStream();
        // 2.  使用流实现图片的写出
        IOUtils.copy(fileInputStream,outputStream);
        fileInputStream.close();
        outputStream.close();
    }


    // 删除图片
    @PostMapping("delete")
    public ResponseResult deletePicture(String uuid) throws Exception{

        return  slideService.deletePicture(uuid);
    }


    // 置顶操作
    @PostMapping("update")
    public ResponseResult topPicture(Integer id) {

        return  slideService.topPicture(id);
    }


    // 前端轮播图的加载
    @GetMapping ("/font/list")
    public ResponseResult fontPictureList(Integer number) {

        return  slideService.fontPictureList(number);
    }

}
